<?php
/**
 * Created by PhpStorm.
 * User: ouryun
 * Date: 2021/5/15
 * Time: 10:52
 */

namespace app\admin\controller;


use think\admin\Controller;

/**
 * 客流统计图表管理
 * Class GuestFlowChart
 * @package app\admin\controller
 */
class GuestFlowChart extends Controller
{
    /**
     * 客流统计图表
     * @auth true
     * @login true
     */
    public function index()
    {
        $data = $this->request->param();
        $dataTime = $data['dateTime'] ?? 'day';
        $whereMap = $where = [];
        if (isset($data['shop_id']) && !empty($data['shop_id'])) {
            $whereMap[] = ['fr.shop_id', '=', $data['shop_id']];
            $where[] = ['shop_id', '=', $data['shop_id']];
        }
        if (isset($data['is_come']) && $data['is_come'] !== '') {
            $whereMap[] = ['e.is_come', '=', $data['is_come']];
            $text = $data['is_come'] == 1 ? '进' : '出';
        } else {
            $text = '进/出';
        }
        //门店数量
        $this->shopTotal = $this->app->db->name('lg_distributor')->where('status', 1)->count('id');
        //统计总数量
        $this->usersTotal = $this->app->db->name('lg_face_recognition')->alias('fr')
            ->join('lg_distributor d', 'fr.shop_id=d.id', 'LEFT')
            ->join('lg_equip e', 'e.equip_id=fr.equipment_id and e.channel_id=fr.channel_id', 'LEFT')
            ->where($whereMap)
            ->count('fr.id');
        //统计进店数量
        $this->comeTotal = $this->app->db->name('lg_face_recognition')->alias('fr')
            ->join('lg_distributor d', 'fr.shop_id=d.id', 'LEFT')
            ->join('lg_equip e', 'e.equip_id=fr.equipment_id and e.channel_id=fr.channel_id', 'LEFT')
            ->where($whereMap)
            ->where('e.is_come', 1)->cache(true, 60)->count('fr.id');
        //统计出店数量
        $this->outTotal = $this->app->db->name('lg_face_recognition')->alias('fr')
            ->join('lg_distributor d', 'fr.shop_id=d.id', 'LEFT')
            ->join('lg_equip e', 'e.equip_id=fr.equipment_id and e.channel_id=fr.channel_id', 'LEFT')
            ->where($whereMap)
            ->where('e.is_come', 2)->cache(true, 60)->count();
        //用户进店日对比
        $todayTotal = $this->app->db->name('lg_face_recognition')->alias('fr')
            ->join('lg_distributor d', 'fr.shop_id=d.id', 'LEFT')
            ->join('lg_equip e', 'e.equip_id=fr.equipment_id and e.channel_id=fr.channel_id', 'LEFT')
            ->where($whereMap)
            ->whereTime('fr.enter_time', 'today')
            ->count('fr.id');
        $yesterdayTotal = $this->app->db->name('lg_face_recognition')->alias('fr')
            ->join('lg_distributor d', 'fr.shop_id=d.id', 'LEFT')
            ->join('lg_equip e', 'e.equip_id=fr.equipment_id and e.channel_id=fr.channel_id', 'LEFT')->cache(true, 60)
            ->where($whereMap)
            ->whereTime('fr.enter_time', 'yesterday')
            ->count('fr.id');
        $this->userDayCompare = [
            ['value' => $todayTotal, 'name' => '今日数据'],
            ['value' => $yesterdayTotal, 'name' => '昨日数据'],
        ];
        //用户进店周对比
        $weekTotal = $this->app->db->name('lg_face_recognition')->alias('fr')
            ->join('lg_distributor d', 'fr.shop_id=d.id', 'LEFT')
            ->join('lg_equip e', 'e.equip_id=fr.equipment_id and e.channel_id=fr.channel_id', 'LEFT')
            ->where($whereMap)
            ->whereWeek('fr.enter_time')
            ->count('fr.id');
        $preWeekTotal = $this->app->db->name('lg_face_recognition')->alias('fr')
            ->join('lg_distributor d', 'fr.shop_id=d.id', 'LEFT')
            ->join('lg_equip e', 'e.equip_id=fr.equipment_id and e.channel_id=fr.channel_id', 'LEFT')
            ->where($whereMap)->cache(true, 60)
            ->whereWeek('fr.enter_time', 'last week')
            ->count('fr.id');
        $this->userWeekCompare = [
            ['value' => $weekTotal, 'name' => '本周数据'],
            ['value' => $preWeekTotal, 'name' => '上周数据'],
        ];
        //用户进店月对比
        $monthTotal = $this->app->db->name('lg_face_recognition')->alias('fr')
            ->join('lg_distributor d', 'fr.shop_id=d.id', 'LEFT')
            ->join('lg_equip e', 'e.equip_id=fr.equipment_id and e.channel_id=fr.channel_id', 'LEFT')
            ->where($whereMap)
            ->whereMonth('fr.enter_time')
            ->count('fr.id');
        $preMonthTotal = $this->app->db->name('lg_face_recognition')->alias('fr')
            ->join('lg_distributor d', 'fr.shop_id=d.id', 'LEFT')
            ->join('lg_equip e', 'e.equip_id=fr.equipment_id and e.channel_id=fr.channel_id', 'LEFT')
            ->where($whereMap)->cache(true, 60)
            ->whereMonth('fr.enter_time', 'last month')
            ->count('fr.id');
        $this->userMonthCompare = [
            ['value' => $monthTotal, 'name' => '本月数据'],
            ['value' => $preMonthTotal, 'name' => '上月数据'],
        ];
        //门店人流统计
        $list = $this->app->db->name('lg_face_recognition')
            ->where('face_id', '<>', '')
            ->where($where)
            ->column('id,count(id) total', 'shop_id');
        $this->shopList = $this->app->db->name('lg_distributor')->where('status', 1)->field('id,name')->select()->toArray();
        foreach ($this->shopList as $key => $value) {
            $this->shopList[$key]['total'] = $list[$value['id']]['total'] ?? 0;
        }
        //当天考勤记录
        $this->normal = $this->app->db->name('lg_check_work_face')->whereTime('punch_time', 'today')->count('id');
        $this->abnormal = $this->app->db->name('lg_shop_check_work_warning')->whereTime('create_time', 'today')->count('id');
        $this->official = $text;
        //
        $query = $this->app->db->name('lg_face_recognition');
        if (isset($data['shop_id']) && !empty($data['shop_id'])) {
            $query->where('shop_id', $data['shop_id']);
        }
        switch ($dataTime) {
            case 'day':
                $query->whereTime('enter_time', 'today');
                break;
            case 'week':
                $query->whereWeek('enter_time');
                break;
            case 'month':
                $query->whereMonth('enter_time');
                break;
            case 'yesterday':
                $query->whereTime('enter_time', 'yesterday');
                break;
            case 'last_week':
                $query->whereTime('enter_time', 'last week');
                break;
            case 'last_month':
                $query->whereTime('enter_time', 'last month');
                break;
            default:
                $query->whereTime('enter_time', 'today');
        }
        $list = $query->field('id,sex,age')->select()->toArray();
        //性别分布
        $genderData = [
            ['value' => 0, 'name' => '男'],
            ['value' => 0, 'name' => '女']
        ];
        //用户年龄
        $ageData = [
            [
                ['name' => '24岁以下', 'value' => 0],
                ['name' => '25-29岁', 'value' => 0],
                ['name' => '30-39岁', 'value' => 0],
                ['name' => '40-49岁', 'value' => 0],
                ['name' => '50岁以下', 'value' => 0]
            ]
        ];
        foreach ($list as $key => $value) {
            if ($value['sex'] == 1) {
                $genderData[0]['value'] += 1;
            } else {
                $genderData[1]['value'] += 1;
            }
            if ($value['age'] < 24) {
                $ageData[0][0]['value'] += 1;
            } elseif ($value['age'] >= 24 && $value['age'] < 29) {
                $ageData[0][1]['value'] += 1;
            } elseif ($value['age'] >= 29 && $value['age'] < 39) {
                $ageData[0][1]['value'] += 1;
            } elseif ($value['age'] >= 39 && $value['age'] < 49) {
                $ageData[0][1]['value'] += 1;
            } else {
                $ageData[0][1]['value'] += 1;
            }
        }
        $this->genderData = json_encode($genderData, JSON_UNESCAPED_UNICODE);
        $this->ageData = json_encode($ageData, JSON_UNESCAPED_UNICODE);
        $this->fetch();
    }

    /**
     * 客流数
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserData()
    {
        $datas = $this->request->param();
        $time = $datas['time'];
        $db = $this->app->db->name('lg_face_recognition');
        if ($time == 'at') {
            $db->whereMonth('enter_time');
        } else {
            $db->whereTime('enter_time', 'last month');
        }
        $list = $db->field('id,count(id) total,FROM_UNIXTIME(enter_time, "%m-%d") AS date')
            ->group('date')
            ->select()->toArray();
        return returnJson('200', '成功', $list);
    }
}